package com.rgs.springboot.selector;

import com.rgs.springboot.autoconfig.RgsAutoConfiguration;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.ServiceLoader;

/**
 * @author RenGaoshuai
 * @date 2024/7/2 13:12
 * @description
 */
public class RgsImportSelect implements DeferredImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {

        //这里使用的是SPI机制,会去读取META-INF/services下的文件里面配置的接口，从而读取到相关的实现类
        ServiceLoader<RgsAutoConfiguration> load = ServiceLoader.load(RgsAutoConfiguration.class);
        ArrayList<String> list = new ArrayList<String>();
        for (RgsAutoConfiguration rgsAutoConfiguration : load) {
            list.add(rgsAutoConfiguration.getClass().getName());
        }
        return list.toArray(new String[list.size()]);
    }
}
